package cn.deepxi.org.controller;

import cn.deepxi.org.domain.Department;
import cn.deepxi.org.query.DeptQuery;
import cn.deepxi.org.service.IDepartmentService;
import cn.deepxi.org.service.impl.DepartmentServiceImpl;
import cn.deepxi.utils.AjaxResult;
import cn.deepxi.utils.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private IDepartmentService departmentService;
    @ApiOperation(value = "新增修改的接口" )
    @PutMapping
    public AjaxResult addOrUpt(@ApiParam(name="传递对象",required = true) @RequestBody Department department){
        try{
            if(department.getId()==null){
                departmentService.add(department);
            }else {
                departmentService.upt(department);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.erro();
    }
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.erro();
        }
    }
    @PostMapping
    public QueryResult<Department> querryPage(@RequestBody DeptQuery deptQuery){
        return departmentService.queryData(deptQuery);
    }
    @GetMapping("/{id}")
    public Department findById(@PathVariable("id") Long id){
            return departmentService.findById(id);
    }
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            departmentService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.erro();

    }
    @GetMapping("/tree")
    public List<Department> findDeptTree(){
        return departmentService.findDeptTree();
    }


}
